using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;
using PCSComUtils.DataAccess;
using PCSComUtils.PCSExc;
using PCSComUtils.Common;
using System.Runtime.InteropServices;
namespace PCSComMaterials.Inventory.DS
{
	public class IV_MaterialReceiptDS 
	{
		public IV_MaterialReceiptDS()
		{
		}
		private const string THIS = "PCSComMaterials.Inventory.DS.IV_MaterialReceiptDS";

	
		///    <summary>
		///       This method uses to add data to IV_MaterialReceipt
		///    </summary>
		///    <Inputs>
		///        IV_MaterialReceiptVO       
		///    </Inputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <History>
		///       Thursday, June 23, 2005
		///    </History>


		public void Add(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";
			 
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS =null;
			try
			{
				IV_MaterialReceiptVO objObject = (IV_MaterialReceiptVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);
				
				strSql=	"INSERT INTO IV_MaterialReceipt("
					+ IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD + ","
					+ IV_MaterialReceiptTable.POSTDATE_FLD + ","
					+ IV_MaterialReceiptTable.RECEIPTTYPE_FLD + ","
					+ IV_MaterialReceiptTable.QUANTITY_FLD + ","
					+ IV_MaterialReceiptTable.UNITCOST_FLD + ","
					+ IV_MaterialReceiptTable.TOTALAMOUNT_FLD + ","
					+ IV_MaterialReceiptTable.LOT_FLD + ","
					+ IV_MaterialReceiptTable.SERIAL_FLD + ","
					+ IV_MaterialReceiptTable.CCNID_FLD + ","
					+ IV_MaterialReceiptTable.BINID_FLD + ","
					+ IV_MaterialReceiptTable.LOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.STOCKUMID_FLD + ","
					+ IV_MaterialReceiptTable.PRODUCTID_FLD + ","
					+ IV_MaterialReceiptTable.TRANSNO_FLD + ","
					+ IV_MaterialReceiptTable.DESCRIPTION_FLD + ","
					+ IV_MaterialReceiptTable.QASTATUS_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERDETAILID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD + ")"
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD].Value = objObject.MaterialReceiptID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.POSTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.POSTDATE_FLD].Value = objObject.PostDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.RECEIPTTYPE_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.RECEIPTTYPE_FLD].Value = objObject.ReceiptType;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.QUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.QUANTITY_FLD].Value = objObject.Quantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.UNITCOST_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.UNITCOST_FLD].Value = objObject.UnitCost;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.TOTALAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.TOTALAMOUNT_FLD].Value = objObject.TotalAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.LOT_FLD].Value = objObject.Lot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.SERIAL_FLD].Value = objObject.Serial;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.CCNID_FLD].Value = objObject.CCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.BINID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.BINID_FLD].Value = objObject.BinID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.LOCATIONID_FLD].Value = objObject.LocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.STOCKUMID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.STOCKUMID_FLD].Value = objObject.StockUMID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.TRANSNO_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.TRANSNO_FLD].Value = objObject.TransNo;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.DESCRIPTION_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.DESCRIPTION_FLD].Value = objObject.Description;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.QASTATUS_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.QASTATUS_FLD].Value = objObject.QAStatus;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.WORKORDERMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERMASTERID_FLD].Value = objObject.WorkOrderMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.WORKORDERDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERDETAILID_FLD].Value = objObject.WorkOrderDetailID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD].Value = objObject.PurchaseOrderMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD].Value = objObject.PurchaseOrderDetailID;


				
				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	

	

		///    <summary>
		///       This method uses to add data to IV_MaterialReceipt
		///    </summary>
		///    <Inputs>
		///        IV_MaterialReceiptVO       
		///    </Inputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <History>
		///       Thursday, June 23, 2005
		///    </History>

		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			string strSql = String.Empty;
			strSql=	"DELETE " + IV_MaterialReceiptTable.TABLE_NAME + " WHERE  " + "MasterLocationID" + "=" + pintID.ToString();
			OleDbConnection oconPCS=null;
			OleDbCommand ocmdPCS =null;
			try
			{
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
				ocmdPCS = null;

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}



			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	

	

		///    <summary>
		///       This method uses to add data to IV_MaterialReceipt
		///    </summary>
		///    <Inputs>
		///        IV_MaterialReceiptVO       
		///    </Inputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <History>
		///       Thursday, June 23, 2005
		///    </History>

		public object GetObjectVO(int pintID)
		{
			const string METHOD_NAME = THIS + ".GetObjectVO()";
			DataSet dstPCS = new DataSet();
			
			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				strSql=	"SELECT "
					+ IV_MaterialReceiptTable.MASTERLOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD + ","
					+ IV_MaterialReceiptTable.POSTDATE_FLD + ","
					+ IV_MaterialReceiptTable.RECEIPTTYPE_FLD + ","
					+ IV_MaterialReceiptTable.QUANTITY_FLD + ","
					+ IV_MaterialReceiptTable.UNITCOST_FLD + ","
					+ IV_MaterialReceiptTable.TOTALAMOUNT_FLD + ","
					+ IV_MaterialReceiptTable.LOT_FLD + ","
					+ IV_MaterialReceiptTable.SERIAL_FLD + ","
					+ IV_MaterialReceiptTable.CCNID_FLD + ","
					+ IV_MaterialReceiptTable.BINID_FLD + ","
					+ IV_MaterialReceiptTable.LOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.STOCKUMID_FLD + ","
					+ IV_MaterialReceiptTable.PRODUCTID_FLD + ","
					+ IV_MaterialReceiptTable.TRANSNO_FLD + ","
					+ IV_MaterialReceiptTable.DESCRIPTION_FLD + ","
					+ IV_MaterialReceiptTable.QASTATUS_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERDETAILID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD
					+ " FROM " + IV_MaterialReceiptTable.TABLE_NAME
					+" WHERE " + IV_MaterialReceiptTable.MASTERLOCATIONID_FLD + "=" + pintID;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				odrPCS = ocmdPCS.ExecuteReader();

				IV_MaterialReceiptVO objObject = new IV_MaterialReceiptVO();

				while (odrPCS.Read())
				{ 
					objObject.MasterLocationID = int.Parse(odrPCS[IV_MaterialReceiptTable.MASTERLOCATIONID_FLD].ToString().Trim());
					objObject.MaterialReceiptID = int.Parse(odrPCS[IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD].ToString().Trim());
					objObject.PostDate = DateTime.Parse(odrPCS[IV_MaterialReceiptTable.POSTDATE_FLD].ToString().Trim());
					objObject.ReceiptType = int.Parse(odrPCS[IV_MaterialReceiptTable.RECEIPTTYPE_FLD].ToString().Trim());
					objObject.Quantity = Decimal.Parse(odrPCS[IV_MaterialReceiptTable.QUANTITY_FLD].ToString().Trim());
					objObject.UnitCost = Decimal.Parse(odrPCS[IV_MaterialReceiptTable.UNITCOST_FLD].ToString().Trim());
					objObject.TotalAmount = Decimal.Parse(odrPCS[IV_MaterialReceiptTable.TOTALAMOUNT_FLD].ToString().Trim());
					objObject.Lot = odrPCS[IV_MaterialReceiptTable.LOT_FLD].ToString().Trim();
					objObject.Serial = odrPCS[IV_MaterialReceiptTable.SERIAL_FLD].ToString().Trim();
					objObject.CCNID = int.Parse(odrPCS[IV_MaterialReceiptTable.CCNID_FLD].ToString().Trim());
					objObject.BinID = int.Parse(odrPCS[IV_MaterialReceiptTable.BINID_FLD].ToString().Trim());
					objObject.LocationID = int.Parse(odrPCS[IV_MaterialReceiptTable.LOCATIONID_FLD].ToString().Trim());
					objObject.StockUMID = int.Parse(odrPCS[IV_MaterialReceiptTable.STOCKUMID_FLD].ToString().Trim());
					objObject.ProductID = int.Parse(odrPCS[IV_MaterialReceiptTable.PRODUCTID_FLD].ToString().Trim());
					objObject.TransNo = odrPCS[IV_MaterialReceiptTable.TRANSNO_FLD].ToString().Trim();
					objObject.Description = odrPCS[IV_MaterialReceiptTable.DESCRIPTION_FLD].ToString().Trim();
					objObject.QAStatus = int.Parse(odrPCS[IV_MaterialReceiptTable.QASTATUS_FLD].ToString().Trim());
					objObject.WorkOrderMasterID = int.Parse(odrPCS[IV_MaterialReceiptTable.WORKORDERMASTERID_FLD].ToString().Trim());
					objObject.WorkOrderDetailID = int.Parse(odrPCS[IV_MaterialReceiptTable.WORKORDERDETAILID_FLD].ToString().Trim());
					objObject.PurchaseOrderMasterID = int.Parse(odrPCS[IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD].ToString().Trim());
					objObject.PurchaseOrderDetailID = int.Parse(odrPCS[IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD].ToString().Trim());

				}		
				return objObject;					
			}
			catch(OleDbException ex)
			{			
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}


		///    <summary>
		///       This method uses to add data to IV_MaterialReceipt
		///    </summary>
		///    <Inputs>
		///        IV_MaterialReceiptVO       
		///    </Inputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <History>
		///       Thursday, June 23, 2005
		///    </History>
		
	
		public void Update(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".Update()";

			IV_MaterialReceiptVO objObject = (IV_MaterialReceiptVO) pobjObjecVO;
			

			//prepare value for parameters
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				strSql=	"UPDATE IV_MaterialReceipt SET "
					+ IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.POSTDATE_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.RECEIPTTYPE_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.QUANTITY_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.UNITCOST_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.TOTALAMOUNT_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.LOT_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.SERIAL_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.CCNID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.BINID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.LOCATIONID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.STOCKUMID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.PRODUCTID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.TRANSNO_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.DESCRIPTION_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.QASTATUS_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.WORKORDERMASTERID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.WORKORDERDETAILID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD + "=   ?" + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD + "=  ?"
					+" WHERE " + IV_MaterialReceiptTable.MASTERLOCATIONID_FLD + "= ?";

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD].Value = objObject.MaterialReceiptID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.POSTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.POSTDATE_FLD].Value = objObject.PostDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.RECEIPTTYPE_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.RECEIPTTYPE_FLD].Value = objObject.ReceiptType;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.QUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.QUANTITY_FLD].Value = objObject.Quantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.UNITCOST_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.UNITCOST_FLD].Value = objObject.UnitCost;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.TOTALAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.TOTALAMOUNT_FLD].Value = objObject.TotalAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.LOT_FLD].Value = objObject.Lot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.SERIAL_FLD].Value = objObject.Serial;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.CCNID_FLD].Value = objObject.CCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.BINID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.BINID_FLD].Value = objObject.BinID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.LOCATIONID_FLD].Value = objObject.LocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.STOCKUMID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.STOCKUMID_FLD].Value = objObject.StockUMID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.TRANSNO_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.TRANSNO_FLD].Value = objObject.TransNo;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.DESCRIPTION_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.DESCRIPTION_FLD].Value = objObject.Description;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.QASTATUS_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.QASTATUS_FLD].Value = objObject.QAStatus;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.WORKORDERMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERMASTERID_FLD].Value = objObject.WorkOrderMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.WORKORDERDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERDETAILID_FLD].Value = objObject.WorkOrderDetailID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD].Value = objObject.PurchaseOrderMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD].Value = objObject.PurchaseOrderDetailID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.MASTERLOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.MASTERLOCATIONID_FLD].Value = objObject.MasterLocationID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}

		}


		///    <summary>
		///       This method uses to add data to IV_MaterialReceipt
		///    </summary>
		///    <Inputs>
		///        IV_MaterialReceiptVO       
		///    </Inputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <History>
		///       Thursday, June 23, 2005
		///    </History>

		public DataSet List()
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();
			
		

			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				strSql=	"SELECT "
					+ IV_MaterialReceiptTable.MASTERLOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD + ","
					+ IV_MaterialReceiptTable.POSTDATE_FLD + ","
					+ IV_MaterialReceiptTable.RECEIPTTYPE_FLD + ","
					+ IV_MaterialReceiptTable.QUANTITY_FLD + ","
					+ IV_MaterialReceiptTable.UNITCOST_FLD + ","
					+ IV_MaterialReceiptTable.TOTALAMOUNT_FLD + ","
					+ IV_MaterialReceiptTable.LOT_FLD + ","
					+ IV_MaterialReceiptTable.SERIAL_FLD + ","
					+ IV_MaterialReceiptTable.CCNID_FLD + ","
					+ IV_MaterialReceiptTable.BINID_FLD + ","
					+ IV_MaterialReceiptTable.LOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.STOCKUMID_FLD + ","
					+ IV_MaterialReceiptTable.PRODUCTID_FLD + ","
					+ IV_MaterialReceiptTable.TRANSNO_FLD + ","
					+ IV_MaterialReceiptTable.DESCRIPTION_FLD + ","
					+ IV_MaterialReceiptTable.QASTATUS_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERDETAILID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD
					+ " FROM " + IV_MaterialReceiptTable.TABLE_NAME;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();
				
				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS,IV_MaterialReceiptTable.TABLE_NAME);

				return dstPCS;
			}
			catch(OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}


		}


		///    <summary>
		///       This method uses to add data to IV_MaterialReceipt
		///    </summary>
		///    <Inputs>
		///        IV_MaterialReceiptVO       
		///    </Inputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <History>
		///       Thursday, June 23, 2005
		///    </History>
		
		public void UpdateDataSet(DataSet pdstData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS =null;
			OleDbCommandBuilder odcbPCS ;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();

			try
			{
				strSql=	"SELECT "
					+ IV_MaterialReceiptTable.MASTERLOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD + ","
					+ IV_MaterialReceiptTable.POSTDATE_FLD + ","
					+ IV_MaterialReceiptTable.RECEIPTTYPE_FLD + ","
					+ IV_MaterialReceiptTable.QUANTITY_FLD + ","
					+ IV_MaterialReceiptTable.UNITCOST_FLD + ","
					+ IV_MaterialReceiptTable.TOTALAMOUNT_FLD + ","
					+ IV_MaterialReceiptTable.LOT_FLD + ","
					+ IV_MaterialReceiptTable.SERIAL_FLD + ","
					+ IV_MaterialReceiptTable.CCNID_FLD + ","
					+ IV_MaterialReceiptTable.BINID_FLD + ","
					+ IV_MaterialReceiptTable.LOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.STOCKUMID_FLD + ","
					+ IV_MaterialReceiptTable.PRODUCTID_FLD + ","
					+ IV_MaterialReceiptTable.TRANSNO_FLD + ","
					+ IV_MaterialReceiptTable.DESCRIPTION_FLD + ","
					+ IV_MaterialReceiptTable.QASTATUS_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERDETAILID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD 
					+ "  FROM " + IV_MaterialReceiptTable.TABLE_NAME;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				pdstData.EnforceConstraints = false;
				odadPCS.Update(pdstData,IV_MaterialReceiptTable.TABLE_NAME);

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
				{

					throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
				}
				
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}

		}
		/// <summary>
		/// Add and return new ID
		/// </summary>
		public int AddAndReturnID(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";
			 
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS =null;
			try
			{
				IV_MaterialReceiptVO objObject = (IV_MaterialReceiptVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);
				
				strSql=	"INSERT INTO IV_MaterialReceipt("
					+ IV_MaterialReceiptTable.MASTERLOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.POSTDATE_FLD + ","
					+ IV_MaterialReceiptTable.RECEIPTTYPE_FLD + ","
					+ IV_MaterialReceiptTable.QUANTITY_FLD + ","
					+ IV_MaterialReceiptTable.UNITCOST_FLD + ","
					+ IV_MaterialReceiptTable.TOTALAMOUNT_FLD + ","
					+ IV_MaterialReceiptTable.LOT_FLD + ","
					+ IV_MaterialReceiptTable.SERIAL_FLD + ","
					+ IV_MaterialReceiptTable.CCNID_FLD + ","
					+ IV_MaterialReceiptTable.BINID_FLD + ","
					+ IV_MaterialReceiptTable.LOCATIONID_FLD + ","
					+ IV_MaterialReceiptTable.STOCKUMID_FLD + ","
					+ IV_MaterialReceiptTable.PRODUCTID_FLD + ","
					+ IV_MaterialReceiptTable.TRANSNO_FLD + ","
					+ IV_MaterialReceiptTable.DESCRIPTION_FLD + ","
					+ IV_MaterialReceiptTable.QASTATUS_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.WORKORDERDETAILID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD + ","
					+ IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD + ")"
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
				strSql += "; SELECT @@IDENTITY as NewID";
				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.MASTERLOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.MASTERLOCATIONID_FLD].Value = objObject.MasterLocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.POSTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.POSTDATE_FLD].Value = objObject.PostDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.RECEIPTTYPE_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.RECEIPTTYPE_FLD].Value = objObject.ReceiptType;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.QUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.QUANTITY_FLD].Value = objObject.Quantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.UNITCOST_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.UNITCOST_FLD].Value = objObject.UnitCost;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.TOTALAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.TOTALAMOUNT_FLD].Value = objObject.TotalAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.LOT_FLD].Value = objObject.Lot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.SERIAL_FLD].Value = objObject.Serial;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.CCNID_FLD].Value = objObject.CCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.BINID_FLD, OleDbType.Integer));
				if (objObject.BinID > 0)
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.BINID_FLD].Value = objObject.BinID;
				}
				else
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.BINID_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.LOCATIONID_FLD].Value = objObject.LocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.STOCKUMID_FLD, OleDbType.Integer));
				if (objObject.StockUMID > 0) 
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.STOCKUMID_FLD].Value = objObject.StockUMID;
				}
				else
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.STOCKUMID_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.TRANSNO_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.TRANSNO_FLD].Value = objObject.TransNo;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.DESCRIPTION_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_MaterialReceiptTable.DESCRIPTION_FLD].Value = objObject.Description;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.QASTATUS_FLD, OleDbType.Integer));
				if (objObject.QAStatus > 0)
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.QASTATUS_FLD].Value = objObject.QAStatus;
				}
				else
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.QASTATUS_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.WORKORDERMASTERID_FLD, OleDbType.Integer));
				if (objObject.WorkOrderMasterID > 0)
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERMASTERID_FLD].Value = objObject.WorkOrderMasterID;
				}
				else
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERMASTERID_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.WORKORDERDETAILID_FLD, OleDbType.Integer));
				if (objObject.WorkOrderDetailID > 0)
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERDETAILID_FLD].Value = objObject.WorkOrderDetailID;
				}
				else
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.WORKORDERDETAILID_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD, OleDbType.Integer));
				if (objObject.PurchaseOrderMasterID > 0) 
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD].Value = objObject.PurchaseOrderMasterID;
				}
				else
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERMASTERID_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD, OleDbType.Integer));
				if (objObject.PurchaseOrderDetailID > 0)
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD].Value = objObject.PurchaseOrderDetailID;
				}
				else
				{
					ocmdPCS.Parameters[IV_MaterialReceiptTable.PURCHASEORDERDETAILID_FLD].Value = DBNull.Value;
				}

				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				object objResult = ocmdPCS.ExecuteScalar();	

				return int.Parse(objResult.ToString());

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}		
		}

		/// <summary>
		/// This method is used to get all the information for a specific material 
		/// </summary>
		/// <param name="pintMaterialReceiptID"></param>
		/// <returns></returns>
		public DataRow GetIVMaterialReceiptInfor(int pintMaterialReceiptID)
		{
			const string METHOD_NAME = THIS + ".GetIVMaterialReceiptInfor()";
			const string VIEW_MATERIAL_RECEIPT = "v_IV_MaterialReceipt";
			DataSet dstPCS = new DataSet();
			
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				strSql=	"SELECT * "
					+ " FROM " + VIEW_MATERIAL_RECEIPT
					+ " WHERE " + IV_MaterialReceiptTable.MATERIALRECEIPTID_FLD + "=" + pintMaterialReceiptID;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();
				
				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS,IV_MaterialReceiptTable.TABLE_NAME);

				return dstPCS.Tables[0].Rows[0];
			}
			catch(OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}


		}

	}
}
